package com.xinqi.modules.chat.favorite.controller.app;

import cn.hutool.core.collection.CollectionUtil;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.chat.favorite.dto.req.CollectContentDTO;
import com.xinqi.modules.chat.favorite.dto.req.CollectDeleteBatchTagDTO;
import com.xinqi.modules.chat.favorite.dto.req.CollectDeleteTagDTO;
import com.xinqi.modules.chat.favorite.dto.req.CollectEditBatchTagDTO;
import com.xinqi.modules.chat.favorite.dto.req.CollectEditTagDTO;
import com.xinqi.modules.chat.favorite.dto.req.FavoriteContentDeleteDTO;
import com.xinqi.modules.chat.favorite.dto.req.FavoriteContentQueryDTO;
import com.xinqi.modules.chat.favorite.dto.rsp.FavoriteContentResultDTO;
import com.xinqi.modules.chat.favorite.dto.rsp.FavoriteTypeResultDTO;
import com.xinqi.modules.chat.favorite.service.FavoriteContentService;
import com.xinqi.modules.chat.favorite.service.FavoriteTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * APP -- 收藏
 *
 * @author: yyzhen
 * @date: 2023/03/23
 */
@Validated
@RestController
@RequestMapping("/app/favorite")
@RequiredArgsConstructor
public class FavoriteContentAppController {

    private final FavoriteContentService favoriteContentService;

    private final FavoriteTypeService favoriteTypeService;

    /**
     * 查询收藏分类列表
     *
     * @Title:
     * @MethodName: typeList
     * @Return com.xinqi.common.base.response.R<java.util.List < com.xinqi.modules.chat.favorite.service.FavoriteTypeService>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 14:19
     */
    @GetMapping("/type/list")
    public R<List<FavoriteTypeResultDTO>> typeList() {
        return R.ok(favoriteTypeService.selectList());
    }

    /**
     * 收藏
     *
     * @Title:
     * @MethodName: collection
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 14:35
     */
    @PostMapping("/collection")
    public R collection(@Validated @RequestBody CollectContentDTO dto) {
        List<FavoriteContentResultDTO> list = favoriteContentService.collection(dto);
        if (CollectionUtil.isNotEmpty(list)) {
            return R.ok(MessageUtils.message("chat.favorite.collection.success"), list);
        }
        return R.fail(MessageUtils.message("chat.favorite.collection.fail"));
    }

    /**
     * 收藏 -- 修改标签
     *
     * @Title:
     * @MethodName: editTag
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 15:02
     */
    @PostMapping("/editTag")
    public R editTag(@Validated @RequestBody CollectEditTagDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        favoriteContentService.editTag(dto);
        return R.ok(MessageUtils.message("chat.favorite.content.edit.tag.success"));
    }

    /**
     * 收藏 -- 多选 -- 添加标签
     *
     * @Title:
     * @MethodName: editBatchTag
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 15:02
     */
    @PostMapping("/editBatchTag")
    public R editBatchTag(@Validated @RequestBody CollectEditBatchTagDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (favoriteContentService.editBatchTag(dto)) {
            return R.ok(MessageUtils.message("chat.favorite.content.add.tag.success"));
        }
        return R.fail(MessageUtils.message("chat.favorite.content.add.tag.fail"));
    }

    /**
     * 收藏 -- 删除标签
     *
     * @Title:
     * @MethodName: delTag
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 15:02
     */
    @PostMapping("/delTag")
    public R delTag(@Validated @RequestBody CollectDeleteTagDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (favoriteContentService.delTag(dto)) {
            return R.ok(MessageUtils.message("chat.favorite.content.del.tag.success"));
        }
        return R.fail(MessageUtils.message("chat.favorite.content.del.tag.fail"));
    }

    /**
     * 收藏 -- 多选 -- 删除标签
     *
     * @Title:
     * @MethodName: delBatchTag
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 15:02
     */
    @PostMapping("/delBatchTag")
    public R delBatchTag(@Validated @RequestBody CollectDeleteBatchTagDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (favoriteContentService.delBatchTag(dto)) {
            return R.ok(MessageUtils.message("chat.favorite.content.del.tag.success"));
        }
        return R.fail(MessageUtils.message("chat.favorite.content.del.tag.fail"));
    }

    /**
     * 收藏 -- 删除
     *
     * @Title:
     * @MethodName: remove
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 15:31
     */
    @GetMapping("/remove/{id}")
    public R remove(@NotNull(message = "{chat.favorite.content.id.not.blank}") @PathVariable("id") Long id) {
        if (favoriteContentService.removeById(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("chat.favorite.content.remove.success"));
        }
        return R.fail(MessageUtils.message("chat.favorite.content.remove.fail"));
    }

    /**
     * 收藏 -- 多选 -- 删除
     *
     * @Title:
     * @MethodName: batchRemove
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 15:31
     */
    @PostMapping("/batchRemove")
    public R batchRemove(@Validated @RequestBody FavoriteContentDeleteDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        if (favoriteContentService.removeByIds(dto)) {
            return R.ok(MessageUtils.message("chat.favorite.content.remove.success"));
        }
        return R.fail(MessageUtils.message("chat.favorite.content.remove.fail"));
    }

    /**
     * 收藏 -- 分页列表
     *
     * @Title:
     * @MethodName: page
     * [pageable, dto]
     * @Return com.xinqi.common.base.response.R<com.xinqi.common.base.response.PageInfo < com.xinqi.modules.chat.favorite.dto.rsp.FavoriteContentResultDTO>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 16:17
     */
    @GetMapping("/page")
    public R<PageInfo<FavoriteContentResultDTO>> page(Param pageable, @Validated FavoriteContentQueryDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.ok(favoriteContentService.selectAppPage(pageable, dto));
    }

    /**
     * 收藏 --详情
     *
     * @Title:
     * @MethodName: info
     * [id]
     * @Return com.xinqi.common.base.response.R<com.xinqi.modules.chat.favorite.dto.rsp.FavoriteContentResultDTO>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/23 16:17
     */
    @GetMapping("/info/{id}")
    public R<FavoriteContentResultDTO> info(@NotNull(message = "{chat.favorite.content.id.not.blank}") @PathVariable("id") Long id) {
        return R.ok(favoriteContentService.selectById(id, LoginHelper.getUserId()));
    }

}
